package com.gxa.controller.commlife;

import com.gxa.common.utils.Result;
import com.gxa.entity.commlife.CouponManagement;
import com.gxa.service.commlife.CouponManagementService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author yjl
 * @since 2022-10-12 11:39:47
 */
@Api(tags = "消费券管理")
@RestController
@RequestMapping("/coupon-management")
public class CouponManagementController {
    @Autowired
    private CouponManagementService couponManagementService;

//    List<CouponManagement> couponManagementList;
//    public CouponManagementController(){this.init();}
//    void init(){
//        couponManagementList= new ArrayList<>();
//        CouponManagement couponManagement = new CouponManagement(1,"餐饮优惠券",30,"24小时内",null,null,100,30,0,0);
//        couponManagementList.add(couponManagement);
//        CouponManagement couponManagement1 = new CouponManagement(2,"餐饮优惠券",30,"24小时内",null,null,100,30,0,0);
//        couponManagementList.add(couponManagement1);
//    }

    @CrossOrigin(origins = "*",maxAge = 3600)//跨域请求配置
    @ApiOperation(value = "查询消费券")
    @GetMapping("list/{pageNum}/{pageSize}")
    public Result list(@PathVariable("pageNum") Integer pageNum, @PathVariable("pageSize") Integer pageSize){
        CouponManagement couponManagement = new CouponManagement();
        couponManagement.setCouponName("");
        try {
            Map<String, Object> map = this.couponManagementService.queryList(couponManagement, pageNum, pageSize);
            Result<Map<String, Object>> result = Result.success(map);
            return result;
        }catch (Exception e){
            e.printStackTrace();
            return Result.failed("查询失败");
        }
    }

    @CrossOrigin(origins = "*",maxAge = 3600)//跨域请求配置
    @PostMapping("search")
    @ApiOperation(value = "搜索消费券")
    public Result search(@RequestBody CouponManagement couponManagement,Integer pageNum,Integer pageSize){
        try {
            Map<String, Object> map = this.couponManagementService.queryList(couponManagement, pageNum, pageSize);
            return Result.success(map);
        }catch (Exception e){
            e.printStackTrace();
            return Result.failed("搜索失败");
        }
    }

    @CrossOrigin(origins = "*",maxAge = 3600)//跨域请求配置
    @ApiOperation(value = "增加消费券")
    @PostMapping("add")
    public Result add(@RequestBody CouponManagement couponManagement){
        try{
            this.couponManagementService.addCouponManagement(couponManagement);
            Result<String> result = Result.success("增加成功");
            result.setMsg("增加成功");
            return result;
        }catch (Exception e){
            e.printStackTrace();
            return Result.failed("增加失败");
        }
    }

    @CrossOrigin(origins = "*",maxAge = 3600)//跨域请求配置
    @ApiOperation(value = "查询当前消费券")
    @GetMapping("preEdit/{id}")
    public Result preEdit(@PathVariable("id") Integer id){
        try {
            CouponManagement couponManagement = this.couponManagementService.queryById(id);
            return Result.success(couponManagement);
        }catch (Exception e){
            e.printStackTrace();
            return Result.failed("查询当前消费券失败");
        }
    }

    @CrossOrigin(origins = "*",maxAge = 3600)//跨域请求配置
    @ApiOperation(value = "修改消费券")
    @PutMapping("edit")
    public Result edit(@RequestBody CouponManagement couponManagement){
        try {
            this.couponManagementService.updateCouponManagement(couponManagement);
            Result<Object> result = Result.success("修改成功");
            result.setMsg("修改成功");
            return result;
        }catch (Exception e){
            e.printStackTrace();
            return Result.failed("修改失败");
        }
    }

    @CrossOrigin(origins = "*",maxAge = 3600)//跨域请求配置
    @ApiOperation(value = "删除消费券")
    @DeleteMapping("delete/{id}")
    public Result delete(@PathVariable("id") Integer id){
      try {
          this.couponManagementService.deleteCouponManagement(id);
          Result<Object> result = Result.success("删除成功");
          result.setMsg("删除成功");
          return result;
      }catch (Exception e){
          e.printStackTrace();
          return Result.failed("删除失败");
      }
    }

}
